package com.example.myapplication;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TransactionFragment extends Fragment {

    private RecyclerView recyclerView;
    private TransactionAdapterffffffffff transactionAdapter;
    private List<TransactionGroup> transactionGroupList;

   private DBHelper dbHelper ;
    private ArrayList<Transaction> transactionList = new ArrayList<Transaction>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_transaction, container, false);

        // 初始化 RecyclerView
        recyclerView = view.findViewById(R.id.recyclerViewTransactions);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(),2));
        dbHelper = new DBHelper(getActivity());

        loadTransactions();
        // 准备数据
        transactionGroupList = prepareTransactionData();

        // 设置适配器
        transactionAdapter = new TransactionAdapterffffffffff(transactionList,getActivity());
        recyclerView.setAdapter(transactionAdapter);

        return view;
    }

    // 准备交易数据并进行分组
    private List<TransactionGroup> prepareTransactionData() {

        return groupTransactions(transactionList); // 分组交易数据
    }

    // 模拟获取交易数据
    @SuppressLint("Range")
    private void loadTransactions() {
        transactionList.clear();
        Cursor cursor = dbHelper.getReadableDatabase().rawQuery(
                "SELECT t.transaction_id, p.name AS product_name, t.buyer_id, t.seller_id, t.status, t.date " +
                        "FROM transactions t " +
                        "JOIN products p ON t.product_id = p.product_id", null);

        if (cursor.moveToFirst()) {
            do {
                Transaction transaction = new Transaction();
                transaction.setTransactionId(cursor.getInt(cursor.getColumnIndex("transaction_id")));
                transaction.setProductName(cursor.getString(cursor.getColumnIndex("product_name")));
                transaction.setBuyerId(cursor.getInt(cursor.getColumnIndex("buyer_id")));
                transaction.setSellerId(cursor.getInt(cursor.getColumnIndex("seller_id")));
                transaction.setStatus(cursor.getString(cursor.getColumnIndex("status")));
                transaction.setDate(cursor.getString(cursor.getColumnIndex("date")));
                transactionList.add(transaction);
            } while (cursor.moveToNext());
        }
        cursor.close();

        transactionAdapter = new TransactionAdapterffffffffff(transactionList, getContext());
        recyclerView.setAdapter(transactionAdapter);
    }

    // 按状态分组交易数据
    private List<TransactionGroup> groupTransactions(List<Transaction> transactions) {
        Map<String, List<Transaction>> groupedMap = new HashMap<>();

        // 按状态分组
        for (Transaction transaction : transactions) {
            String status = transaction.getStatus();
            if (!groupedMap.containsKey(status)) {
                groupedMap.put(status, new ArrayList<>());
            }
            groupedMap.get(status).add(transaction);
        }

        // 创建 TransactionGroup 列表
        List<TransactionGroup> transactionGroups = new ArrayList<>();
        for (Map.Entry<String, List<Transaction>> entry : groupedMap.entrySet()) {
            transactionGroups.add(new TransactionGroup(entry.getKey(), entry.getValue()));
        }

        return transactionGroups;
    }
}